//
//  LLYFifthViewController.m
//  ZZ15091031LiuLeiYan
//
//  Created by qianfeng001 on 15/8/28.
//  Copyright (c) 2015年 刘雷岩. All rights reserved.
//

#import "LLYFifthViewController.h"

#import "LLYAddViewController.h"
#import "LLYModel.h"

@interface LLYFifthViewController () <UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    
    NSMutableArray *_dataSource;
    
    
}

@end

@implementation LLYFifthViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    _dataSource = [NSMutableArray array];
    
    [self createTableView];
    [self createDataSource];
    [self createItem];
}

- (void)createDataSource {
    
}

- (void)createTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667-64-49) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    
    [self.view addSubview:_tableView];
}

- (void)createItem {
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"+" style:UIBarButtonItemStylePlain target:self action:@selector(addData:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithTitle:@"-" style:UIBarButtonItemStylePlain target:self action:@selector(deleteData:)];
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)deleteData:(UIBarButtonItem *)item {
    if (_tableView.isEditing == NO) {
        [_tableView setEditing:YES animated:YES];
        item.title = @"完成";
    }else if(_tableView.isEditing ==YES){
        [_tableView setEditing:NO animated:YES];
        item.title = @"-";
    }
}

- (void)addData:(UIBarButtonItem *)item {
    if (_tableView.isEditing) {
        [_tableView setEditing:NO animated:YES];
        self.navigationItem.rightBarButtonItem.title = @"-";
    }
    LLYAddViewController *addVC = [[LLYAddViewController alloc] init];
    addVC.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    
    addVC.block = ^(NSString *name,NSString *age) {
        LLYModel *model = [[LLYModel alloc] init];
        model.text = name;
        model.detailText = age;
        [_dataSource addObject:model];
        [_tableView reloadData];
    };
    
    
    [self presentViewController:addVC animated:YES completion:^{
        
    }];
}


#pragma mark -
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cellId";
    
    UITableViewCell *cell = [_tableView dequeueReusableCellWithIdentifier:cellId];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellId];
    }
    LLYModel *model = _dataSource[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:@"campfire"];
    cell.textLabel.text = model.text;
    cell.detailTextLabel.text = model.detailText;
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [_dataSource removeObjectAtIndex:indexPath.row];
        [_tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    }
}


@end
